<?php
namespace app\web\model;

use think\Model;
use think\Log;
use think\Db;
use app\web\service\MaoyanApi as my;
/**
 * 猫眼会员
 */
class MaoYan extends Model {
    /**
     * 开通猫眼会员
     */
    public function record($sms,$param=[])
    {
        $skucode = isset($param['skucode']) ? $param['skucode']:'';
        $mobile = isset($param['mobile']) ? $param['mobile']:'';
        $channelOrderId = isset($param['channelOrderId']) ? $param['channelOrderId']:'';
        $location = isset($param['location']) ? $param['location']:'';
        $getType = isset($param['getType']) ? $param['getType']:0;
        $payMoney = isset($param['payMoney']) ? $param['payMoney']:0;
        $smsCode = isset($param['smsCode']) ? $param['smsCode']:'';
        $reserved1 = isset($param['reserved1']) ? $param['reserved1']:'';
        if ($getType) {
            $res = Db::name('act_my_record')->where(['order_sn'=>$channelOrderId,'mobile'=>$mobile])->whereTime("creat_time",'month')->count();
        } else {
            $res = Db::name('act_my_record')->where(['order_sn'=>$channelOrderId,'mobile'=>$mobile])->count();
        }
        if (!$res) {
            $info = Db::name('act_my_list')->where(['code_id'=>$skucode])->find();
            if ($info) {
                $act_id = $info['m_id'];
                $lifeCycle = $info['type']?$info['type']:31;
                $channelId = $info['channel_id'];
                $uip = '';
                $code_reserved1 = 0;
                $insertData = [
                    'act_id' => $act_id,
                    'mobile' => $mobile,
                    'order_sn' => $channelOrderId,
                    'lifeCycle' => $lifeCycle,
                    'payMoney' => $payMoney,
                    'code' => $code_reserved1,
                    'operator'=> $location['operator'],
                    'home_location'=> $location['home_location'],
                    'channel_id' => $channelId,
                    'creat_time' => date('Y-m-d H:i:s')
                ];
                $r_id = Db::name('act_my_record')->insertGetId($insertData);
                if ($r_id){
                    $my = new my();
                    $params = [
                        'mobileNo' => $mobile,
                        'lifeCycle' => 31,
                        'channelOrderId' => $channelOrderId,
                        'actionType' => 0,
                        'payMoney' => 0
                    ];
                    $result = $my->gatewayMemberCreate($params);
                    $return = json_decode($result,true);
                    $code = isset($return['data']['code'])?$return['data']['code']:-1;
                    if ($code == '5000000'){
                        $sms_data = [
                            'mobile'    => $mobile,
                            'code'    => "MYACCOUNT",
                            'word'    => [],
                            'send' => 1
                        ];
                        $sms_text = $sms->sms($sms_data,0,0);
                    } else {
                        $sms_data = [
                            'mobile'    => $mobile,
                            'code'    => "JHFAIL",
                            'word'    => [],
                            'send' => 1
                        ];
                        $sms_text = $sms->sms($sms_data,0,0);
                    }
                    Db::name("act_my_record")->where(['order_sn'=>$channelOrderId,'mobile'=>$mobile])->update(['code'=>$code,'active_time'=>date('Y-m-d H:i:s')]);
                    $sms_data = [
                        'mobile'    => $mobile,
                        'code'    => "MYACCOUNT",
                        'word'    => [],
                        'send' => 1
                    ];
                    $sms_text = $sms->sms($sms_data,0,0);
                    $body = ["resultCode"=>"1","resultMsg"=>"成功","recoId"=>""];
                    return $body;
                }
            }
        } 
        $body = ["resultCode"=>"2","resultMsg"=>"开通失败","recoId"=>""];
        return $body;
    }

}